package com.doge.walknovel.business.home.discover

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.setPadding
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.doge.wnpro.R
import com.doge.walknovel.api.model.BannerInfo
import com.doge.walknovel.api.model.NovelInfo
import com.doge.walknovel.api.model.StrategyInfo
import com.doge.walknovel.api.model.process
import com.doge.walknovel.business.novel.NovelDetailActivity
import com.doge.walknovel.business.novel.NovelRefer
import com.doge.walknovel.business.search.SearchActivity
import com.doge.walknovel.business.strategy.StrategyNovelActivity
import com.doge.walknovel.core.BaseFragment
import com.doge.walknovel.core.base.isRefreshing
import com.doge.wnpro.databinding.FragmentDiscoverBinding
import com.doge.walknovel.utils.setOnClickCallback
import com.doge.walknovel.utils.systemWindowInsetsTop

class DiscoverFragment : BaseFragment() {
    private var innerBinding: FragmentDiscoverBinding? = null
    private val binding get() = innerBinding!!
    private val viewModel by viewModels<DiscoverViewModel> { DiscoverViewModelFactory() }
    private val adapter by lazy { DiscoverAdapter() }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        innerBinding =
            innerBinding ?: FragmentDiscoverBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        systemWindowInsetsTop.observe(viewLifecycleOwner) {
            binding.fitsSys.setPadding(it / 2)
        }
        binding.search.setOnClickCallback {
            SearchActivity.start(it.context)
        }
        binding.recyclerView.itemAnimator = null
        binding.recyclerView.layoutManager =
            GridLayoutManager(binding.recyclerView.context, 3).apply {
                spanSizeLookup = DiscoverLayoutManagerSpanSizeLookUp(adapter)
            }
        binding.recyclerView.adapter = adapter
        adapter.setListener(::handleAdapterCallback)
        binding.refreshLayout.setColorSchemeResources(R.color.pull_refresh)
        binding.refreshLayout.setOnRefreshListener {
            viewModel.request()
        }
        viewModel.loading.observe(viewLifecycleOwner) {
            binding.refreshLayout.isRefreshing = it.isRefreshing()
        }
        viewModel.dataList.observe(viewLifecycleOwner) {
            adapter.submitList(it)
        }
        viewModel.request()
    }


    private fun handleAdapterCallback(action: String, item: Any?) {
        when (action) {
            BANNER -> (item as? BannerInfo?).process(context)
            STRATEGY_MORE -> StrategyNovelActivity.start(context, item as? StrategyInfo?)
            BOOK_CLICKED -> NovelDetailActivity.start(context, (item as? NovelInfo?)?.novelId, NovelRefer.discover, (item as? NovelInfo?)?.appReferId)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.search.setOnClickListener(null)
        binding.refreshLayout.setOnRefreshListener(null)
        binding.recyclerView.adapter = null
        adapter.setListener(null)
        innerBinding = null
    }
}